//
//  CYCouponsViewController.m
//  cyjf
//
//  Created by tauCross on 2017/5/14.
//  Copyright © 2017年 Auto Financing. All rights reserved.
//

#import "CYCouponsViewController.h"

#import "CYCouponsContentViewController.h"



@interface CYCouponsViewController ()

@property(nonatomic, strong)UIView *topView;
@property(nonatomic, strong)UIView *topLine;
@property(nonatomic, strong)UILabel *normalTitle;
@property(nonatomic, strong)UILabel *usedTitle;
@property(nonatomic, strong)UILabel *disabledTitle;
@property(nonatomic, strong)UIView *currentLine;
@property(nonatomic, strong)UILabel *currentTitle;
@property(nonatomic, strong)UIButton *normalButton;
@property(nonatomic, strong)UIButton *usedButton;
@property(nonatomic, strong)UIButton *disabledButton;

@property(nonatomic, strong)CYCouponsContentViewController *available;
@property(nonatomic, strong)CYCouponsContentViewController *used;
@property(nonatomic, strong)CYCouponsContentViewController *disable;

@property(nonatomic, strong)UIScrollView *scrollView;

@end


@implementation CYCouponsViewController

- (void)close
{
    [MobClick event:UM_EVENT_0069];
    [super class];
}

- (void)loadUI
{
    self.navigationItem.title = @"我的优惠券";
    self.tableView.hidden = YES;
    
    self.topView = [[UIView alloc] init];
    self.topView.backgroundColor = [UIColor whiteColor];
    [self.contentView addSubview:self.topView];
    {
        self.topLine = [UIView cy_lineWithColor:HEXCOLOR(@"ECECEC")];
        [self.topView addSubview:self.topLine];
        
        self.normalTitle = [self titleLabelWithText:@"未使用"];
        [self.topView addSubview:self.normalTitle];
        
        self.usedTitle = [self titleLabelWithText:@"已使用"];
        [self.topView addSubview:self.usedTitle];
        
        self.disabledTitle = [self titleLabelWithText:@"已失效"];
        [self.topView addSubview:self.disabledTitle];
        
        self.currentTitle = self.normalTitle;
        self.currentTitle.font = [UIFont systemFontOfSize:16 weight:UIFontWeightMedium];
        self.currentTitle.alpha = 1;
        
        self.currentLine = [UIView cy_lineWithColor:HEXCOLOR(@"444444")];
        [self.topView addSubview:self.currentLine];
        
        self.normalButton = [[UIButton alloc] init];
        [self.normalButton addTarget:self action:@selector(onNormalButtonTouchUpInside) forControlEvents:UIControlEventTouchUpInside];
        [self.topView addSubview:self.normalButton];
        
        self.usedButton = [[UIButton alloc] init];
        [self.usedButton addTarget:self action:@selector(onUsedButtonTouchUpInside) forControlEvents:UIControlEventTouchUpInside];
        [self.topView addSubview:self.usedButton];
        
        self.disabledButton = [[UIButton alloc] init];
        [self.disabledButton addTarget:self action:@selector(onDisabledButtonTouchUpInside) forControlEvents:UIControlEventTouchUpInside];
        [self.topView addSubview:self.disabledButton];
    }
    
    self.scrollView = [[UIScrollView alloc] init];
    self.scrollView.pagingEnabled = YES;
    self.scrollView.showsVerticalScrollIndicator = NO;
    self.scrollView.showsHorizontalScrollIndicator = NO;
    self.scrollView.bounces = NO;
    self.scrollView.delegate = self;
    [self.contentView addSubview:self.scrollView];
    {
        self.available = [[CYCouponsContentViewController alloc] init];
        self.available.status = CYDataAPICouponStatusAvailable;
        [self addChildViewController:self.available];
        [self.scrollView addSubview:self.available.view];
        
        self.used = [[CYCouponsContentViewController alloc] init];
        self.used.status = CYDataAPICouponStatusUsed;
        [self addChildViewController:self.used];
        [self.scrollView addSubview:self.used.view];
        
        self.disable = [[CYCouponsContentViewController alloc] init];
        self.disable.status = CYDataAPICouponStatusDisable;
        [self addChildViewController:self.disable];
        [self.scrollView addSubview:self.disable.view];
    }
    [self.scrollView.panGestureRecognizer requireGestureRecognizerToFail:self.navigationController.interactivePopGestureRecognizer];
}

- (void)layoutUI
{
    self.topView.width = self.contentView.width;
    self.topView.height = 40;
    self.topView.left = 0;
    self.topView.top = 0;
    {
        self.topLine.width = self.topView.width;
        self.topLine.height = 1 / SCREEN_SCALE;
        self.topLine.left = 0;
        self.topLine.bottom = self.topView.height;
        
        self.normalTitle.centerY = self.topView.height / 2;
        self.normalTitle.centerX = self.topView.width / 6;
        
        self.usedTitle.centerY = self.topView.height / 2;
        self.usedTitle.centerX = self.topView.width / 2;
        
        self.disabledTitle.centerY = self.topView.height / 2;
        self.disabledTitle.centerX = self.topView.width * 5 / 6;
        
        self.currentLine.height = 2;
        self.currentLine.width = self.currentTitle.width - 4;
        self.currentLine.bottom = self.topView.height;
        self.currentLine.centerX = self.currentTitle.centerX;
        
        self.normalButton.width = self.topView.width / 3;
        self.normalButton.height = self.topView.height;
        self.normalButton.top = 0;
        self.normalButton.left = 0;
        
        self.usedButton.frame = self.normalButton.frame;
        self.usedButton.left = self.normalButton.right;
        
        self.disabledButton.frame = self.usedButton.frame;
        self.disabledButton.left = self.usedButton.right;
    }
    
    self.scrollView.width = self.contentView.width;
    self.scrollView.height = self.contentView.height - self.topView.bottom;
    self.scrollView.left = 0;
    self.scrollView.top = self.topView.bottom;
    self.scrollView.contentSize = CGSizeMake(self.scrollView.width * 3, self.scrollView.height);
    {
        self.available.view.frame = self.scrollView.bounds;
        self.available.view.left = 0;
        
        self.used.view.frame = self.scrollView.bounds;
        self.used.view.left = self.scrollView.width;
        
        self.disable.view.frame = self.scrollView.bounds;
        self.disable.view.left = self.scrollView.width * 2;
    }
}

#pragma mark - Control Events
- (void)onNormalButtonTouchUpInside
{
    if(self.currentTitle == self.normalTitle)
    {
        return;
    }
    [self animateToTitle:self.normalTitle];
}

- (void)onUsedButtonTouchUpInside
{
    if(self.currentTitle == self.usedTitle)
    {
        return;
    }
    [self animateToTitle:self.usedTitle];
}

- (void)onDisabledButtonTouchUpInside
{
    if(self.currentTitle == self.disabledTitle)
    {
        return;
    }
    [self animateToTitle:self.disabledTitle];
}

#pragma mark - UIScrollViewDelegate
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    if(scrollView == self.scrollView)
    {
        if(self.topView.userInteractionEnabled == NO)
        {
            self.normalTitle.alpha = MIN(1, MAX(0.545, 1 - (ABS(scrollView.contentOffset.x) / scrollView.width) * 0.455));
            self.usedTitle.alpha = MIN(1, MAX(0.545, 1 - (ABS(scrollView.contentOffset.x - scrollView.width) / scrollView.width) * 0.455));
            self.disabledTitle.alpha = MIN(1, MAX(0.545, 1 - (ABS(scrollView.contentOffset.x - scrollView.width * 2) / scrollView.width) * 0.455));
            self.currentLine.centerX = (scrollView.contentOffset.x / (scrollView.contentSize.width - scrollView.width) * 4 + 1) / 6 * self.topView.width;
        }
    }
}

- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
{
    if(scrollView == self.scrollView)
    {
        self.topView.userInteractionEnabled = NO;
    }
}

- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate
{
    if(scrollView == self.scrollView)
    {
        if(!decelerate)
        {
            self.topView.userInteractionEnabled = YES;
            [self checkCurrentTitle];
        }
    }
}

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    if(scrollView == self.scrollView)
    {
        self.topView.userInteractionEnabled = YES;
        [self checkCurrentTitle];
    }
}

#pragma mark - Private Method
- (UILabel *)titleLabelWithText:(NSString *)text
{
    UILabel *label = [[UILabel alloc] init];
    label.textColor = HEXCOLOR(@"444444");
    label.alpha = 0.545;
    label.text = text;
    label.textAlignment = NSTextAlignmentCenter;
    label.font = [UIFont systemFontOfSize:16];
    [label sizeToFit];
    label.width += 4;
    label.height += 4;
    return  label;
}

- (UITableView *)createTableView
{
    UITableView *tableView = [[UITableView alloc] init];
    tableView.backgroundColor = [UIColor clearColor];
    tableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 0, 10)];
    tableView.tableHeaderView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 0, 10)];
    tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    tableView.delegate = self;
    return tableView;
}

- (void)checkCurrentTitle
{
    self.normalTitle.alpha = 0.545;
    self.usedTitle.alpha = 0.545;
    self.disabledTitle.alpha = 0.545;
    UILabel *title = nil;
    if(self.scrollView.contentOffset.x == 0)
    {
        title = self.normalTitle;
    }
    else if(self.scrollView.contentOffset.x == self.scrollView.width)
    {
        title = self.usedTitle;
    }
    else
    {
        title = self.disabledTitle;
    }
    self.currentTitle.font = [UIFont systemFontOfSize:16];
    self.currentTitle = title;
    self.currentTitle.font = [UIFont systemFontOfSize:16 weight:UIFontWeightMedium];
    self.currentTitle.alpha = 1;
    self.currentLine.centerX = self.currentTitle.centerX;
}

- (void)animateToTitle:(UILabel *)title
{
    APP_LOCK_USER_INTERACTION;
    @weakify(self);
    CGFloat targetX = 0;
    if(title == self.usedTitle)
    {
        targetX = self.scrollView.width;
    }
    if(title == self.disabledTitle)
    {
        targetX = self.scrollView.width * 2;
    }
    [UIView animateWithDuration:0.25
                          delay:0
                        options:UIViewAnimationOptionCurveEaseInOut
                     animations:^{
                         @strongify(self);
                         self.currentLine.centerX = title.centerX;
                         self.scrollView.contentOffset = CGPointMake(targetX, 0);
                         self.currentTitle.alpha = 0.545;
                         title.alpha = 1;
                     }
                     completion:^(BOOL finished) {
                         @strongify(self);
                         self.currentTitle.font = [UIFont systemFontOfSize:16];
                         self.currentTitle = title;
                         self.currentTitle.font = [UIFont systemFontOfSize:16 weight:UIFontWeightMedium];
                         APP_UNLOCK_USER_INTERACTION;
                     }];
}

@end
